package com.itheima.controller;

import com.itheima.dto.EmpDto;
import com.itheima.pojo.Emp;
import com.itheima.pojo.MyResponseResult;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 员工管理Controller
 */
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    public MyResponseResult getByPage(@RequestParam(defaultValue = "1")Integer page,
                                      @RequestParam(defaultValue = "1")Integer pageSize,EmpDto empDto){
        PageBean<Emp> pageBean = empService.getByPage(page,pageSize,empDto);
        return MyResponseResult.ok(pageBean);
    }
    @DeleteMapping({"/{ids}"})
    public MyResponseResult<Object> deleteById(@PathVariable Integer[] ids){
     int i = empService.deleteById(ids);
     return i>1?MyResponseResult.ok("成功了"):MyResponseResult.error(Arrays.toString(ids)+"删除失败");
    }

    @PostMapping
    public MyResponseResult insertEmp(@RequestBody Emp emp){
        int i =  empService.insertEmp(emp);
        return i>=1?MyResponseResult.ok("成功了"):MyResponseResult.error("保存失败");
    }

    @GetMapping("/{id}")
    public MyResponseResult<Emp> getById(@PathVariable("id") Integer id) {
        Emp emp = empService.getById(id);
        return MyResponseResult.ok(emp);
    }
//     @PutMapping
//     private MyResponseResult<Object> upDate(@RequestBody Emp emp){
//         empService.upDate(emp);
//        return MyResponseResult.ok("修改成功了");
//    }
}
